﻿using System;
using System.ComponentModel;
using ApplicationGenerator.Forms.Actions;

namespace ApplicationGenerator.Forms.Forms
{
    public class ButtonActionTypeConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if(sourceType == typeof(ActionBase))
            {
                return true;
            }

            if (sourceType == typeof(string))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value is ActionBase)
            {
                return true;
            }

            return false;
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return base.CanConvertTo(context, destinationType);
        }

        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (value != null && value is ActionBase && destinationType == typeof(string))
            {
                string actionName = ActionStrategyChooser.GetActionName(value as ActionBase);
                if (actionName != null)
                {
                    return actionName;
                }
            }

            return "Brak" ;
        }
    }
}
